package cn.newgrand.netcall.transcript;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.html.HTMLEditorKit;

import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.component.BackgroundPanel;
import org.jivesoftware.spark.ui.VCardPanel;
import org.jivesoftware.spark.ui.transcript.MessageTranscriptPane;
import org.jivesoftware.spark.util.GraphicUtils;
import org.jivesoftware.spark.util.log.Log;
/**
 * 此类用于展示用户的历史聊天记录信息、历史广播消息等
 * 
 * @author SidneyRen
 *
 */
public class HistoryMessagePanel extends JFrame {
	private static JPanel mainPanel;
	private MessageTranscriptPane window;
	private VCardPanel vCardPanel;
	private JPanel topPanel;
	//		
	private JPanel searchPanel;
	private JComboBox yearbox;
	private JComboBox monthbox;
	private JComboBox daybox;
	private JLabel labelyear;
	private JLabel labelmonth;
	private JLabel labelday;
	private JButton searchButton;
	private String searchKey;
	private JLabel broadcastLabel;
	final private JScrollPane pane;
	
//	

	public HistoryMessagePanel(String title) {
		searchPanel=getSearchPanel();
		searchPanel.setOpaque(false);
		mainPanel = new BackgroundPanel();
		mainPanel.setLayout(new BorderLayout());
		
		topPanel = new JPanel();
		topPanel.setOpaque(false);
		topPanel.setLayout(new GridBagLayout());
		// TODO
		if(title.equals("broadcast")){
			broadcastLabel = new JLabel(Res.getString("label.broadcast.hitory.name"));
			topPanel.add(broadcastLabel, new GridBagConstraints(0, 0, 1, 0, 1.0, 1.0,
					GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
					new Insets(5, 0, 5, 0), 0, 0));
		}else if(title.equals("appsys")){
			broadcastLabel = new JLabel("待办任务");
			topPanel.add(broadcastLabel, new GridBagConstraints(0, 0, 1, 0, 1.0, 1.0,
					GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
					new Insets(5, 20, 5, 0), 0, 0));
		}else{
			vCardPanel = new VCardPanel(title);
			topPanel.add(vCardPanel, new GridBagConstraints(0, 0, 1, 0, 1.0, 1.0,
					GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
					new Insets(5, 0, 5, 0), 0, 0));
		}
		topPanel.add(searchPanel, new GridBagConstraints(1, 0, 1, 0, 1.0, 1.0,
				GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
				new Insets(5, 0, 5, 0), 0, 0));

//		mainPanel.add(topPanel, BorderLayout.NORTH);

		window = new MessageTranscriptPane();
		window.setEditorKit(new HTMLEditorKit());
		window.setBackground(Color.white);
		pane = new JScrollPane(window);
		pane.getVerticalScrollBar().setBlockIncrement(50);
		pane.getVerticalScrollBar().setUnitIncrement(20);

		mainPanel.add(pane, BorderLayout.CENTER);
		setIconImage(SparkRes.getImageIcon(SparkRes.HISTORY_16x16).getImage());
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(mainPanel, BorderLayout.CENTER);
		pack();
		setSize(350, 500);
		window.setCaretPosition(0);
		GraphicUtils.centerWindowOnScreen(this);
		setVisible(true);
		
		//TODO 将搜索条的时间定为当前的年月日
		Calendar calendar=Calendar.getInstance();
		int day=calendar.get(Calendar.DAY_OF_MONTH);
		int month=calendar.get(Calendar.MONTH)+1;
		int year=calendar.get(Calendar.YEAR);
		for (int i = 0; i <daybox.getItemCount(); i++) {
			int d=Integer.parseInt(daybox.getItemAt(i).toString());
			if(day==d){
				daybox.setSelectedIndex(i);
			}
		}
		for (int i = 0; i <monthbox.getItemCount(); i++) {
			int m=Integer.parseInt(monthbox.getItemAt(i).toString());
			if(month==m){
				monthbox.setSelectedIndex(i);
			}
		}
		for (int i = 0; i <yearbox.getItemCount(); i++) {
			int y=Integer.parseInt(yearbox.getItemAt(i).toString());
			if(year==y){
				yearbox.setSelectedIndex(i);
			}
		}
		//

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				window.setText("");
			}
		});

		window.setEditable(false);
		// TODO
		
	}


	public MessageTranscriptPane getTranscriptPane() {
		return window;
	}

	public void setAllTitle(String title) {
//		if (title.equals("broadcast")) {
////			setTitle(Res.getString("label.broadcast.history.title"));
//			setTitle(Res.getString("title.message.transcript"));
//		} else fi(){
//			setTitle(Res.getString("title.history.for", title));
//		}
		setTitle("待办任务");
		setSearchKey(title);

	}
	//获取搜索条的面板
	public JPanel getSearchPanel() {
		JPanel searchPanel=new JPanel();

		String[] years;
		String[] months;
		String[] days;
		yearbox = new JComboBox();
		monthbox = new JComboBox();
		daybox = new JComboBox();
		searchButton = new JButton(Res.getString("ok"));
		ArrayList<String> yearlist = new ArrayList<String>();
		for (int i = 0; i < 30; i++) {
			yearlist.add(i + 2008 + "");
		}
		years = (String[]) yearlist.toArray(new String[yearlist.size()]);
		ArrayList<String> monthlist = new ArrayList<String>();
		for (int i = 0; i < 12; i++) {
			monthlist.add(i + 1 + "");
		}
		months = (String[]) monthlist.toArray(new String[monthlist.size()]);
		ArrayList<String> daylist = new ArrayList<String>();
		for (int i = 0; i < 31; i++) {
			daylist.add(i + 1 + "");
		}
		days = (String[]) daylist.toArray(new String[daylist.size()]);

		for (int i = 0; i < years.length; i++) {
			yearbox.addItem(years[i]);

		}
		for (int i = 0; i < months.length; i++) {
			monthbox.addItem(months[i]);
		}
		for (int i = 0; i < days.length; i++) {
			daybox.addItem(days[i]);
		}
		// TODO Auto-generated constructor stub
		labelyear = new JLabel(Res.getString("combobox.searchpanel.year"));
		labelmonth = new JLabel(Res.getString("combobox.searchpanel.month"));
		labelday = new JLabel(Res.getString("combobox.searchpanel.day"));

		setLayout(new FlowLayout());
//		setSize(100, 100);
		setVisible(true);

		searchPanel.add(yearbox);
		searchPanel.add(labelyear);
		searchPanel.add(monthbox);
		searchPanel.add(labelmonth);
		searchPanel.add(daybox);
		searchPanel.add(labelday);
		searchPanel.add(searchButton);
		searchPanel.setBackground(Color.WHITE);
		// 搜索按钮监听
		searchButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// JOptionPane.showMessageDialog(null,"Welcome to Spark");
				String year = yearbox.getSelectedItem().toString();
				String month = monthbox.getSelectedItem().toString();
				String day = daybox.getSelectedItem().toString();
				
				final String tempDate = year +"-"+ month + "-" + day;
				DateFormat f1 = new SimpleDateFormat("yyyy-MM-dd");
				Date date = null;
				try {
					date = f1.parse(tempDate);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				// TODO
				HistoryMessageManager.getInstance()
						.showSearchResult(searchKey, date,getMessageHistoryPanel());

			}
		});
		 return searchPanel;
		 
	}

	public void setSearchKey(String str) {
		searchKey = str;
	}
	public HistoryMessagePanel getMessageHistoryPanel(){
		return this;
	}
	
	/**
     * Scrolls the chat window to the bottom.
     */
    public void scrollToBottom() {
//        if (mousePressed) {
//            return;
//        }

        int lengthOfChat = window.getDocument().getLength();
        window.setCaretPosition(lengthOfChat);

        try {
        	JScrollBar scrollBar = pane.getVerticalScrollBar();
        	scrollBar.setValue(scrollBar.getMaximum());
        }
        catch (Exception e) {
            Log.error(e);
        }
    }

}
